﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProgramWindow.View
{
    /// <summary>
    /// ProgramView.xaml 的交互逻辑
    /// </summary>
    public partial class ProgramView : UserControl
    {
        public static ViewModel.ProgramViewModel viewModel = new ViewModel.ProgramViewModel();
        DAL.ProgramDAL programDAL = null;
        BaseModel.MesDb.HkcProgramBind bindModel = null;
        BaseModel.MesDb.HkcProgramData dataModel = null;
        MainWindow _window = null;
        public ProgramView(MainWindow window,ref bool isOpen)
        {
            try
            {
                InitializeComponent();
                DataContext = viewModel;
                programDAL = new DAL.ProgramDAL();
                _window = window;
                isOpen = BLL.AppBLL.GetAPP(window);
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var binds = programDAL.GetBinds();
                viewModel.Types.Clear();
                foreach (var item in binds)
                {
                    viewModel.Types.Add(item.type);
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }
        private void FocusTextBox(TextBox textBox)
        {
            textBox.IsEnabled = true;
            _window.Activate();
            _window.Focus();
            textBox.Focus();
            textBox.SelectAll();
        }
        private void Fail(TextBox textBox)
        {
            try
            {
                dataModel.result = "NG";
                dataModel.user = MainWindow.viewModel.User;
                dataModel.time = programDAL.db.GetDate().ToString("G");
                if (programDAL.InsertData(dataModel) == false)
                {
                    HandyControl.Controls.Growl.Warning("数据上传失败");
                }
                FocusTextBox(textBox);
                viewModel.StateColor = Brushes.Tomato;
                viewModel.State = "失败";
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }

}
        private void Pass(TextBox textBox)
        {
            try
            {
                dataModel.result = "OK";
                dataModel.user = MainWindow.viewModel.User;
                dataModel.time = programDAL.db.GetDate().ToString("G");
                if (programDAL.InsertData(dataModel) == false)
                {
                    HandyControl.Controls.Growl.Warning("数据上传失败");
                    viewModel.StateColor = Brushes.Tomato;
                    viewModel.State = "失败";
                    FocusTextBox(textBox);
                    return;
                }
                FocusTextBox(textBox);
                viewModel.StateColor = Brushes.ForestGreen;
                viewModel.State = "成功";
                
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }

        }
        private void Wait(TextBox textBox)
        {
            Dispatcher.Invoke(() =>
            {
                viewModel.StateColor = Brushes.Gold;
                viewModel.State = "等待";
                _window.Activate();
            });
        }
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                TextBox textBox = (sender as TextBox);
                Wait(textBox);
                if (e.Key==Key.Enter)
                {
                    textBox.IsEnabled = false;
                    BaseTool.CommonHelper.Delay(100);
                    //bindModel = new BaseModel.MesDb.HkcProgramBind
                    //{
                    //    mcu = "NUC126NE4AE",
                    //    ldrom = "e32d",
                    //    aprom = "9835"
                    //};
                    if (bindModel == null)
                    {
                        HandyControl.Controls.Growl.Warning("未选择机型");
                        FocusTextBox(textBox);
                        return;
                    }
                    string code = textBox.Text.Trim();
                    string type = BLL.ProgramBLL.GetType(code);
                    if (type == "")
                    {
                        HandyControl.Controls.Growl.Warning("条码错误");
                        FocusTextBox(textBox);
                        return;
                    }
                    if (type != viewModel.Type)
                    {
                        HandyControl.Controls.Growl.Warning("机型不匹配");
                        FocusTextBox(textBox);
                        return;
                    }
                    dataModel = new BaseModel.MesDb.HkcProgramData();
                    dataModel.sn = code;
                    dataModel.mcu = viewModel.Mcu;
                    dataModel.aprom = viewModel.Aprom;
                    dataModel.ldrom = viewModel.Ldrom;

                    if (BLL.AppBLL.CheckEditText(bindModel.mcu) == false)
                    {
                        HandyControl.Controls.Growl.Warning("芯片不匹配");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(200);
                    if (bindModel.ldrom != "" && BLL.AppBLL.CheckText("校验值: "+bindModel.ldrom) == false)
                    {
                        HandyControl.Controls.Growl.Warning("LDROM不匹配");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(200);
                    if (bindModel.aprom != "" && BLL.AppBLL.CheckText("校验值: " + bindModel.aprom) == false)
                    {
                        HandyControl.Controls.Growl.Warning("APROM不匹配");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(200);
                    bool ldromCheck = BLL.AppBLL.GetCheckButton("LDROM");
                    if (bindModel.ldrom != "" && ldromCheck == false)
                    {
                        HandyControl.Controls.Growl.Warning("LDROM勾选错误");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(200);
                    if (bindModel.ldrom == "" && ldromCheck == true)
                    {
                        HandyControl.Controls.Growl.Warning("LDROM勾选错误");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(200);
                    bool apromCheck = BLL.AppBLL.GetCheckButton("APROM");
                    if (bindModel.aprom != "" && apromCheck == false)
                    {
                        HandyControl.Controls.Growl.Warning("LDROM勾选错误");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(500);
                    if (bindModel.aprom == "" && apromCheck == true)
                    {
                        HandyControl.Controls.Growl.Warning("LDROM勾选错误");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(500);
                    viewModel.State = "烧录";
                    bool isClick =  BLL.AppBLL.ClickButton("开始");
                    if (isClick==false)
                    {
                        HandyControl.Controls.Growl.Warning("开始烧录失败");
                        FocusTextBox(textBox);
                        return;
                    }
                    BaseTool.CommonHelper.Delay(1000);
                    bool program= BLL.AppBLL.Program();
                    if (program==false)
                    {
                        HandyControl.Controls.Growl.Warning("烧录失败");
                        Fail(textBox);
                        return;
                    }
                    Pass(textBox);
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }

        private void ComboBox_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (viewModel.Type != "")
                {
                    bindModel = programDAL.GetBindModel(viewModel.Type);
                    viewModel.Mcu = bindModel.mcu;
                    viewModel.Ldrom = bindModel.ldrom;
                    viewModel.Aprom = bindModel.aprom;
                }
                else
                {
                    bindModel = null;
                    viewModel.Mcu = "";
                    viewModel.Ldrom = "";
                    viewModel.Aprom = "";
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }

        private void ComboBox_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                var binds = programDAL.GetBinds();
                viewModel.Types.Clear();
                foreach (var item in binds)
                {
                    viewModel.Types.Add(item.type);
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.Growl.Error(ex.Message);
                BaseTool.LogHelper.WriteError("错误", ex);
            }
        }
    }
}
